using UnityEngine;

namespace ZSW.Framework.Utils
{
    public static class ZSWFUtil_Math
    {
        /// <summary>
        /// 重映射
        /// </summary>
        /// <param name="value"></param>
        /// <param name="old_min"></param>
        /// <param name="old_max"></param>
        /// <param name="new_min"></param>
        /// <param name="new_max"></param>
        /// <returns></returns>
        public static float Remap(this float value, float old_min, float old_max, float new_min, float new_max)
        {
            old_min = Mathf.Min(old_min, old_max);
            old_max = Mathf.Max(old_min, old_max);
            new_min = Mathf.Min(new_min, new_max);
            new_max = Mathf.Max(new_min, new_max);
            value = Mathf.Clamp(value, old_min, old_max);
            float value_scale = (value - old_min) / (old_max - old_min);
            return new_min + (new_max - new_min) * value_scale;
        }

        /// <summary>
        /// 限制数值范围
        /// </summary>
        /// <param name="value"></param>
        /// <param name="range"></param>
        /// <returns></returns>
        public static float Clamp(this float value, Vector2 range)
        {
            float min = Mathf.Min(range.x, range.y);
            float max = Mathf.Max(range.x, range.y);
            return Mathf.Clamp(value, min, max);
        }

        /// <summary>
        /// 是否在范围内
        /// </summary>
        /// <param name="range"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool InRange(float value, Vector2 range)
        {
            float min = Mathf.Min(range.x, range.y);
            float max = Mathf.Max(range.x, range.y);
            return InRange(value, min, max);
        }

        /// <summary>
        /// 是否在范围内
        /// </summary>
        /// <param name="value"></param>
        /// <param name="min"></param>
        /// <param name="max"></param>
        /// <returns></returns>
        public static bool InRange(float value, float min, float max)
        {
            return value >= min && value <= max;
        }

        /// <summary>
        /// 在范围内随机
        /// </summary>
        /// <param name="range"></param>
        /// <returns></returns>
        public static float GetRandomInRange(Vector2 range)
        {
            float min = Mathf.Min(range.x, range.y);
            float max = Mathf.Max(range.x, range.y);
            return GetRandomInRange(min, max);
        }

        /// <summary>
        /// 在范围内随机
        /// </summary>
        /// <param name="min"></param>
        /// <param name="max"></param>
        /// <returns></returns>
        public static float GetRandomInRange(float min, float max)
        {
            return Random.Range(min, max);
        }

        public static int GetRandomInRange(Vector2Int range)
        {
            int min = Mathf.Min(range.x, range.y);
            int max = Mathf.Max(range.x, range.y);
            return GetRandomInRange(min, max);
        }

        public static int GetRandomInRange(int min, int max)
        {
            return Random.Range(min, max);
        }
    }
}